package com.infra.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.infra.server.entity.SysUser;
import com.infra.server.entity.vo.SysUserVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: zzd
 * @Date: 2020/9/19 16:19
 * @Description: 用户具体详情
 */
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 获取所有用户
     */
    @Select("select su.*,sua.* from sys_user su,sys_user_auth sua where su.id = sua.user_id and sua.is_del != '1'")
    List<SysUserVo> getUserList();

    /**
     * 分页获取用户列表信息
     */
    @Select("select su.*,sua.* from sys_user su,sys_user_auth sua where su.id = sua.user_id and sua.is_del != '1'")
    IPage<SysUserVo> getUserList2(@Param("page") IPage<SysUserVo> page);

    /**
     * 带条件模糊查询
     */
    @Select("select su.*,sua.* from sys_user su,sys_user_auth sua where su.id = sua.user_id " +
            "and (su.realname like #{content} or su.phone like #{content} or su.nickname like #{content} or su.addr like #{content} )")
    IPage<SysUserVo> getUserList3(@Param("page") IPage<SysUserVo> page,@Param("content") String content);

    /**
     * 删除用户
     */
    @Delete("delete sys_user,sys_user_auth,sys_user_role from sys_user left join sys_user_auth on sys_user.id=sys_user_auth.user_id " +
            "left join sys_user_role on sys_user.id = sys_user_role.user_id where sys_user.id = #{id}")
    int delUser(@Param("id") Long id);

    /**
     * 根据用户id获取用户角色
     */
    @Select("select s.role_id from sys_user_role s where s.user_id = #{userId}")
    List<Integer> getRole(Long userId);

    /**
     * 删除用户角色
     */
    @Delete("delete from sys_user_role s where s.user_id = #{userId}")
    int delUserRole(Long userId);

    /**
     * 添加用户角色
     */
    @Insert("<script> " +
            "insert into sys_user_role(user_id,role_id) values" +
            "<foreach collection='roleId' item='item' index='index' separator=','>" +
            "(#{userId},#{item})" +
            "</foreach>" +
            "</script>")
    int addUserRole(@Param("userId") Long userId, @Param("roleId") List<Integer> roleId);

}
